package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.pojo.GetEquipmentsEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * sichaoqi on 2018/12/14
 */
public class ADA_DeviceRecyclerAdapter extends RecyclerView.Adapter<ADA_DeviceRecyclerAdapter.DeviceViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<GetEquipmentsEntity.ListBean> mList;
    private OnItemClickListener itemClickListener;
    private boolean isType = false;

    public ADA_DeviceRecyclerAdapter(Context context) {
        this.mContext = context;
        mList = new ArrayList<>();
        mInflater = LayoutInflater.from(mContext);
    }

    /**
     * 设置list数据
     * @param list
     */
    public void setmList(List<GetEquipmentsEntity.ListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 根据type设置是否显示选择框
     * @param isType
     */
    public void setType (boolean isType){
        this.isType = isType;
        notifyDataSetChanged();
    }

    /**
     * 删除设备数据
     * @return
     */
    public List<GetEquipmentsEntity.ListBean> getmList(){
        return mList;
    }

    /**
     * 删除选中的设备并刷新
     */
    public void deleteDevice(){
        for (int i=0; i<mList.size(); i++){
            GetEquipmentsEntity.ListBean bean = mList.get(i);
            if (bean.isSelected()){
                mList.remove(bean);
            }
        }
       setType(false);
    }

    @Override
    public DeviceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DeviceViewHolder(mInflater.inflate(R.layout.item_device_recycler, parent, false));
    }

    @Override
    public void onBindViewHolder(final DeviceViewHolder holder, int position) {
        final GetEquipmentsEntity.ListBean listBean = mList.get(position);
        //设备号
        holder.tvItemDeviceNumber.setText(listBean.getEquipmentCode());
        //设备名  0-游泳圈，1-体重称
        if ("0".equals(listBean.getEquipmentType())){
            holder.tvItemDeviceName.setText("游泳圈");
        }else{
            holder.tvItemDeviceName.setText("体重称");
        }

        //日期
        holder.tvItemDeviceDate.setText(listBean.getStartTime());

        //是否显示前面的选择框
        if (isType){
            holder.ivItemDeviceSelect.setVisibility(View.VISIBLE);
        }else{
            holder.ivItemDeviceSelect.setVisibility(View.GONE);
        }

        //item点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isType){
                    if (listBean.isSelected()){
                        listBean.setSelected(false);
                        holder.ivItemDeviceSelect.setSelected(false);
                    }else{
                        listBean.setSelected(true);
                        holder.ivItemDeviceSelect.setSelected(true);
                    }
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class DeviceViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_item_device_select)
        ImageView ivItemDeviceSelect;
        @BindView(R.id.tv_item_device_number)
        TextView tvItemDeviceNumber;
        @BindView(R.id.tv_item_device_name)
        TextView tvItemDeviceName;
        @BindView(R.id.tv_item_device_date)
        TextView tvItemDeviceDate;
        public DeviceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface OnItemClickListener {
        void onClick(String id);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
